package com.app.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.app.biz.domain.BizAnnualShandongTopTenIndustrialCounty;
import com.app.biz.domain.BizGycpcxl;
import com.app.biz.excel.entity.BizGycpcxlExcelEntity;
import com.app.biz.mapper.BizAnnualShandongTopTenIndustrialCountyMapper;
import com.app.biz.service.IBizAnnualShandongTopTenIndustrialCountyService;
import com.app.common.core.domain.AjaxResult;
import com.app.common.utils.DateUtils;
import com.app.common.utils.SecurityUtils;
import com.app.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BizAnnualShandongTopTenIndustrialCountyServiceImpl implements IBizAnnualShandongTopTenIndustrialCountyService {

    @Autowired
    private BizAnnualShandongTopTenIndustrialCountyMapper bizAnnualShandongTopTenIndustrialCountyMapper;


    @Override
    public BizAnnualShandongTopTenIndustrialCounty selectBizAnnualShandongTopTenIndustrialCountyByBaseId(String baseId) {
        return bizAnnualShandongTopTenIndustrialCountyMapper.selectBizAnnualShandongTopTenIndustrialCountyByBaseId(baseId);
    }

    @Override
    public List<BizAnnualShandongTopTenIndustrialCounty> selectBizAnnualShandongTopTenIndustrialCountyList(BizAnnualShandongTopTenIndustrialCounty bizAnnualShandongTopTenIndustrialCounty) {
        return bizAnnualShandongTopTenIndustrialCountyMapper.selectBizAnnualShandongTopTenIndustrialCountyList(bizAnnualShandongTopTenIndustrialCounty);
    }

    @Override
    public int insertBizAnnualShandongTopTenIndustrialCounty(BizAnnualShandongTopTenIndustrialCounty bizAnnualShandongTopTenIndustrialCounty) {

        bizAnnualShandongTopTenIndustrialCounty.setBaseId(IdUtil.fastUUID());
        bizAnnualShandongTopTenIndustrialCounty.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        bizAnnualShandongTopTenIndustrialCounty.setCreateTime(new Date());
        return bizAnnualShandongTopTenIndustrialCountyMapper.insertBizAnnualShandongTopTenIndustrialCounty(bizAnnualShandongTopTenIndustrialCounty);
    }

    @Override
    public int updateBizAnnualShandongTopTenIndustrialCounty(BizAnnualShandongTopTenIndustrialCounty bizAnnualShandongTopTenIndustrialCounty) {
        bizAnnualShandongTopTenIndustrialCounty.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        bizAnnualShandongTopTenIndustrialCounty.setUpdateTime(new Date());
        return bizAnnualShandongTopTenIndustrialCountyMapper.updateBizAnnualShandongTopTenIndustrialCounty(bizAnnualShandongTopTenIndustrialCounty);
    }

    @Override
    public int deleteBizAnnualShandongTopTenIndustrialCountyByBaseIds(String[] baseIds) {
        return bizAnnualShandongTopTenIndustrialCountyMapper.deleteBizAnnualShandongTopTenIndustrialCountyByBaseIds(baseIds);
    }

    @Override
    public int deleteBizAnnualShandongTopTenIndustrialCountyByBaseId(String baseId) {
        return bizAnnualShandongTopTenIndustrialCountyMapper.deleteBizAnnualShandongTopTenIndustrialCountyByBaseId(baseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importExcel(String absoluteExcelPath, String year) {
        List<BizAnnualShandongTopTenIndustrialCounty> excelDataArgs = EasyExcel.read(absoluteExcelPath).head(BizAnnualShandongTopTenIndustrialCounty.class).headRowNumber(1).sheet().doReadSync();
        if(!CollectionUtils.isEmpty(excelDataArgs)){
            excelDataArgs = excelDataArgs.stream().filter(f-> !StringUtils.isEmpty(f.getCountyName())).collect(Collectors.toList());
        }
        if(!CollectionUtils.isEmpty(excelDataArgs)){
            for(BizAnnualShandongTopTenIndustrialCounty item : excelDataArgs){
                item.setFillYear(year);
                item.setCreateTime(DateUtils.getNowDate());
                item.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                item.setBaseId(IdUtil.simpleUUID());
            }
            bizAnnualShandongTopTenIndustrialCountyMapper.batchInsertBizAnnualShandongTopTenIndustrialCounty(excelDataArgs);
        }else{
            return AjaxResult.success("上传文档内容不能为空");
        }

        return AjaxResult.success();

    }

    
}
